package servlet;

//import java.awt.Button;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Model.Appointment;

/**
 * Servlet implementation class RoleTable
 */
@WebServlet("/AppttypeTable")
public class AppttypeTable extends HttpServlet {
	
	//Button bEdit, bDelete;
	private static final long serialVersionUID = 1L;
    JspWriter out;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AppttypeTable() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public AppttypeTable(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//bEdit = new Button("edit");
		//bDelete = new Button("delete");
		
		ArrayList<Appointment> apptTable = new ArrayList<Appointment>();
		apptTable = Database.AppointmentDB.getAllAppointments();
		
		for(int i = 0; i < apptTable.size(); i++)
		{
			String responsibilities = apptTable.get(i).getResponsibilities();
			if(responsibilities == null)
			{
				responsibilities = "-";
			}
			else if(responsibilities.length() > 100)
			{
				responsibilities = responsibilities.substring(0, 99) + "...";
			}
			
			out.print("<tr>");
				out.print("<td width='20%'>");
				out.println("<a href=\"appttype_modifyappt.jsp?action=e&id= "+apptTable.get(i).getId()+"\">");
				out.print(apptTable.get(i).getName() + "</a>");
				out.print("</td>");
				
				out.print("<td style='width:50%'>");
				out.print(responsibilities);
				out.print("</td>");
				
				out.print("<td style='width:20%'>");
				out.print(apptTable.get(i).getDateUpdated());
				out.print("</td>");
				
				out.print("<td style='width:10%'>");
				//out.print("<a href=\"appttype_modifyappt.jsp?action=e&id= "+apptTable.get(i).getId()+"\"> <img src='images/edit3.png' alt='images'/> </a>");
				out.print("<a href=\"appttype_modifyappt.jsp?action=d&id= "+apptTable.get(i).getId()+"\"> <img src='images/icn_trash.png' alt='images'/> </a>");
				out.print("</td>");
			out.print("</tr>");
		}
	}

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			// TODO Auto-generated method stub
			String formURL = "appttype_modifyappt.jsp";

			if (request.getParameter("submit").equals("Edit"))
			{
				// Edit
				formURL += "action=e";
			}
			else
			{
				// Delete
			formURL += "action=d";
		}

			String id = request.getParameter("staff");

			formURL += ("&id=" + id);

			response.sendRedirect(formURL);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}



	
